//
//  GLViewController.m
//  V3d_Cocoa
//
//  Created by Y my on 11-3-14.
//  Copyright 2011 NJUST. All rights reserved.
//

#import "GLViewController.h"
//#import "MyNavigationController.h" // Won't need this in IOS development


@implementation GLViewController
@synthesize glView;
@synthesize rotateH, rotateV;
@synthesize my4DImage, mipData;

#define MOVE_UP    1
#define MOVE_DOWN  2
#define MOVE_LEFT  3
#define MOVE_RIGHT 4
#define RESET 0

#define ROTATE_HORIZONAL  0
#define ROTATE_VERTICAL   1

- (void)setData:(My4DImage*)data {
	if (my4DImage) {
		[my4DImage release];
	}
	if (mipData) {
		[mipData release];
	}
	
	my4DImage = data;
	
#ifndef _TEST_
	if (my4DImage) {
		[my4DImage retain];
		mipData = [[MIPData alloc] initWithRawData:(unsigned char****)[my4DImage getData]
										  X_LENGTH:[my4DImage xDim] 
										  Y_LENGTH:[my4DImage yDim]  
										  Z_LENGTH:[my4DImage zDim]
									  ChannelCount:[my4DImage cDim] ];
	}
#else
	mipData = [[MIPData alloc] initWithRawData:(unsigned char****)NULL
									  X_LENGTH:0 
									  Y_LENGTH:0  
									  Z_LENGTH:0
								  ChannelCount:0 ];
#endif
}

/////////// ViewControllerDelegate ///////////////

- (void)viewDidLoad {
	[self.glView setRendererData:self.mipData];
	[glView afterRotationWithInterfaceOrientation:self.interfaceOrientation];
}

- (void)viewDidUnload {
	[self.glView release];
}

- (UIView*)getView {
	return self.view;
}

////////// init & dealloc ////////////////////

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
		lastRotateH = 0;
		lastRotateV = 0;
	}
	return self;
}

- (void)dealloc {
	//[glViewData release];
	[my4DImage release];
	[mipData release];
		
	[super dealloc];
}

/////////////// UI funtions ////////////

/*- (IBAction)back:(UIButton*)sender {
	//MyNavigationController *navCon = [MyNavigationController getInstance];
	[self.navigationController popViewController];
}
 */

- (IBAction)OnMove:(UIButton*)sender
{
	NSInteger senderId = [sender tag];
	switch (senderId)
	{
		case MOVE_UP:
			[glView translateX:0 Y:50];
			break;
		case MOVE_DOWN:
			[glView translateX:0 Y:-50];
			break;
		case MOVE_LEFT:
			[glView translateX:-50 Y:0];
			break;
		case MOVE_RIGHT:
			[glView translateX:50 Y:0];
			break;
		default:
			break;
	}
}

- (IBAction)OnReset:(UIButton*)sender
{
	NSInteger senderId = [sender tag];
	if(senderId == RESET)
	{
		[self.glView reset];
		lastRotateH = 0;
		lastRotateV = 0;
		[rotateH setValue:0];
		[rotateV setValue:0];
	}
}


- (IBAction)OnRotate:(UISlider*)sender
{
	NSInteger senderId = [sender tag];
	
	switch (senderId)
	{
		case ROTATE_HORIZONAL:
			if ([sender value] == lastRotateH)
			{
				break;
			}
			[glView rotateX:5 Y:0 Angle:([sender value]-lastRotateH) / 180.0 * 3.1415 ];
			//[glView rotateX:0 Y:5 Angle:3.1415 ];
			lastRotateH = [sender value];
			break;
		case ROTATE_VERTICAL:
			if ([sender value] == lastRotateV)
			{
				break;
			}
			[glView rotateX:0 Y:5 Angle:([sender value]-lastRotateV) / 180.0 * 3.1415 ];
			//[glView rotateX:5 Y:0 Angle:3.1415 ];
			lastRotateV = [sender value];
			break;

		default:
			break;
	}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
	return YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	[glView afterRotationWithInterfaceOrientation:self.interfaceOrientation];
}

@end
